var List = (function () {
    return {
        editEntity: function ($this) {
            var id = $($this).parents("tr").attr("data-tt-id");
            window.location.href = root + "/security/resource/input.do?id=" + id;
        },
        deleteEntity: function ($this) {
            var id = $($this).parents("tr").attr("data-tt-id");
            var isLeaf = jQuery($this).parents("tr").hasClass("leaf");
            if (!isLeaf) {
                $().toastmessage('showToast', {
                    text: "此资源包含子资源，不能删除！",
                    stayTime: 3000,
                    sticky: false,
                    position: 'top-center',
                    type: 'warning'
                });
            } else{
                deleteResource(id, "确认要删除此资源吗？")
            }

        },
    }
})();

function add() {
    window.location.href = root + "/security/resource/input.do";
}

function deleteResource(id, message) {
    bootbox.confirm({
        message: message,
        buttons: {
            confirm: {
                label: "确认",
                className: "btn-primary btn-sm",
            },
            cancel: {
                label: "取消",
                className: "btn-sm",
            }
        },
        callback: function (result) {
            if (result) {
                jQuery.ajax({
                    'url': root + "/security/resource/delete.do",
                    'traditional': true,
                    'data': {
                        id: id
                    },
                    'dataType': 'text',
                    'type': 'post',
                    'success': function (rs) {
                        var back_url = root + "/security/resource/list.do";
                        toastMessage(rs, back_url)
                    },
                    'error': function () {
                        jQuery("#taskDetail").hideLoading();
                        bootbox.dialog({
                            message: "系统繁忙，请稍后重试。",
                            buttons: {
                                "success": {
                                    "label": "确定",
                                    "className": "btn-sm btn-primary"
                                }
                            }
                        });
                    },
                    'async': true
                });
            }
        }
    });
}